class ManagersController < ApplicationController

  before_filter :authenticate_employee! 

  def employee_list
     @employees = Employee.where("id != ? and is_admin = ? ",current_employee.id,false)
  end
  def change_active_status
    if params[:type] == "employee"
      @user = Employee.find(params[:id])
      check_active_status(@user,true)
    elsif params[:type] == "client"
      @user = Client.find(params[:id])
      check_active_status( @user )
    end
  end

 def convert_to_manager
    if params[:type] == "employee"
      	@user = Employee.find(params[:id])
     	  manager_status(@user,true)
     elsif params[:type] == "client"
      	 @user = Client.find(params[:id])
     		 manager_status( @user )
   		 respond_to do |format|
     	 format.js
      	format.html
    		end
    	end
 	 end

  def client_list
    @clients = current_employee.get_client
  end

  def edit_client 
    @client = Client.find(params[:id])
  end

  def client_update
    @client = Client.find(params[:id])
    respond_to do |format|
      if @client.update_attributes(params[:client])
        format.html { redirect_to :clients, :notice => 'Client was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit_client" }
        format.json { render :json => @client.errors, :status => :unprocessable_entity }
      end
    end
  end

  def assign_clients
   	@employees = Employee.find(:all, :conditions => ["is_manager = ? && is_admin = ? && is_active=?", false,false,true ] ) 
   	@employee = Employee.find_by_id(params[:employee_id])
   	@client =  Client.find(:all, :conditions => ["is_active=?",true ] ) 
    if !params[:employee_id].nil?
     	 if !params[:clients].nil? &&  !params[:clients].blank?
        	 params[:clients].each  do |client_id|
         			client = Client.find(client_id)
          		client.employee_id = params[:employee_id]
							client.save(:validate => false) 
         			flash[:notice] = "Assigned Employee Successfully"
         		end
     		 end
    	end
    respond_to do |format|
        format.js
        format.html 
      end
  end

  def assign_documents
     @client =  Client.find(:all, :conditions => ["is_active=?",true ] ) 
    		if !params[:client_id].nil?
      		client = Client.find(params[:client_id])

					 if !client.employee_id.nil?  || !client.employee_id== 0
        			documents = params[:document_ids].nil? ? Array.new : Document.where("id in (?)", params[:document_ids])

    					 if !documents.blank?
     							copy_url
        					documents.each do |document|

      						 ClientDocument.create(:client_id => params[:client_id], :document_id => document.id,:attachment_file_name=>document.attachment_file_name,:attachment_content_type=>document.attachment_content_type,:attachment_file_size=>document.attachment_file_size,:attachment_updated_at=>document.attachment_updated_at, :status => "Incomplete")

          					DocumentNotification.doc_incomplete(client,document).deliver

          					 flash[:notice] = "Assigned Document Successfully"
        					end
     						 elsif documents.blank?
          					flash[:alert] = "Please Choose A Document"
       					end
   						else
      						flash[:alert] = "No employee assigned to client"
    				end
 					 redirect_to assign_documents_path 
    		end
  	end

  def status_assign_doc
    client_ids= current_employee.clients.collect(&:id).flatten
    @doc_clients = ClientDocument.where("client_id in (?) ", client_ids)
  end

 def copy_url
      client = Client.find(params[:client_id])
      document = Document.find(params[:document_ids])
      document.each do |doc|
         	path_integer =  doc.attachment.to_s
        	 path =path_integer.split("?").map {|s| s.gsub("?"," ") }
        	 read_path="#{Rails.root}/public" + path[0] 
         	 new_path =  "#{Rails.root}/public" + "/#{client.id}" 
        	 Dir.mkdir("#{new_path}") unless File.exist?("#{new_path}")
        	 FileUtils.copy("#{read_path}","#{new_path}")
       end
   end



  private

  def check_active_status(user,is_employee=false)
		if !user.is_active.nil? && user.is_active == false
      user.is_active = true 
    else
      user.is_active = false 
		end
    user.save(:validate => false)
  end 

  def manager_status(user,is_employee=false)
 		if !user.is_manager.nil? && user.is_manager == false || !user.is_active.nil? && user.is_active == false
      user.is_manager = true 
    else
      user.is_manager = false 
    end
    user.save(:validate => false) 
  end 





end
